package com.bdrm.controller.admin;

import com.bdrm.constant.JwtClaimsConstant;
import com.bdrm.context.BaseContext;
import com.bdrm.dto.EmployeeDTO;
import com.bdrm.dto.EmployeeLoginDTO;
import com.bdrm.dto.EmployeePageQueryDTO;
import com.bdrm.dto.PasswordEditDTO;
import com.bdrm.entity.Employee;
import com.bdrm.properties.JwtProperties;
import com.bdrm.result.PageResult;
import com.bdrm.result.Result;
import com.bdrm.service.EmployeeService;
import com.bdrm.utils.JwtUtil;
import com.bdrm.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@Api(tags ="员工信息接口")
@RequestMapping("/admin/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private JwtProperties jwtProperties;
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO){
        log.info("员工登录：{}",employeeLoginDTO);
        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功，生成jwt令牌
        Map<String, Object> map = new HashMap<>();
        map.put(JwtClaimsConstant.EMP_ID,employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                map);


                //通过员工ID，用户名设，工姓名生成的token
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();
        return Result.success(employeeLoginVO);
    }

    @ApiOperation("退出接口")
    @PostMapping("/logout")
    public Result logout(){
        return Result.success();
    }

    @ApiOperation("添加员工")
    @PostMapping("/save")
    public Result save(@RequestBody EmployeeDTO employeeDTO){
        employeeService.save(employeeDTO);
        return Result.success();
    }

    @ApiOperation("员工分页查询")
    @PostMapping("/page")
    public Result<PageResult> page(@RequestBody EmployeePageQueryDTO employeePageQueryDTO){
        PageResult pageResult = employeeService.pageQuery(employeePageQueryDTO);
        return Result.success(pageResult);
    }

    @ApiOperation("员工修改信息")
    @PutMapping("/update")
    public Result update(@RequestBody EmployeeDTO employeeDTO){
        employeeService.update(employeeDTO);
        return Result.success();
    }

    @ApiOperation("根据id查询用户")
    @PostMapping("/{id}")
    public Result<Employee> getByIdUser(@PathVariable Long id){
        Employee employee = employeeService.getByIdUser(id);
        return Result.success(employee);
    }

    @ApiOperation("根据id删除员工账号")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        employeeService.deleteById(id);
        return Result.success();
    }

    @ApiOperation("启用禁用员工账号")
    @PostMapping("/{status}/{id}")
    public Result updateStatus(@PathVariable Integer status,@PathVariable Long id){
        employeeService.updateStatus(status, id);
        return Result.success();
    }

    @ApiOperation("修改密码")
    @PostMapping("/updatePwd")
    public Result updatePwd(@RequestBody PasswordEditDTO passwordEditDTO){
            employeeService.pwdEdit(passwordEditDTO);
            return Result.success();
    }
}
